import ajax from '../../utils/request'
Page({
    data: {
        active: '视频',
        list: [],
        currPage: 1 // 添加 currPage 状态
    },
    onShow: function () {
        this.getList('/video/list', {
            currPage: this.data.currPage // 使用 data 中的 currPage
        });
    },
    onChange(e) {
        // 切换分类时，重置 currPage 为 1
        this.setData({
            currPage: 1
        });
        if (e.detail.name === '视频') {
            this.getList('/video/list', {
                currPage: 1
            })
        } else {
            this.getList('/info/list', {
                currPage: 1
            })
        }
    },
    // 获取数据
    async getList(url, data) {
        try {
            let res = await ajax(`http://43.136.85.99:3008${url}`, data);
            if (data.currPage === 1) {
                this.setData({
                    list: res,
                });
            } else {
                this.setData({
                    list: this.data.list.concat(res),
                });
            }
        } catch (error) {
            console.error('请求数据时出现错误:', error);
        }
    },
    // 页面上拉触底事件的处理函数
    onReachBottom() {
        // 页面滑动到底部时，将 currPage 加 1
        let newCurrPage = this.data.currPage + 1;
        this.setData({
            currPage: newCurrPage
        });
        let url = this.data.active === '视频' ? '/video/list' : '/info/list';
        this.getList(url, {
            currPage: newCurrPage
        });
    }
})